﻿<# 
.SYNOPSIS 
    Invokes a TMAdmin command.
.DESCRIPTION 
    Use this command to invoke the tmadmin.exe executable with the correct environment.
.NOTES 
    File Name  : Invoke-TMAdmin.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Invoke-TMAdmin -TUXDIR $TUXDIR -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain "DOMAIN1" -Command "psr"
    Executes the psr command on DOMAIN1
.EXAMPLE 
    Invoke-TMAdmin -TUXDIR $TUXDIR -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain "DOMAIN1" -Command @("pclt","psr")
    Executes the pclt command, then the psr command on DOMAIN1
.PARAMETER TUXDIR
   The full path to the TUXDIR for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER ServerType
    The type of server on which to perform the action.
.PARAMETER Domain
    The name of the domain on which to perform the action.
.PARAMETER Command
    The command line arguments to pass to the psadmin.exe program.
    See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/task_UsingthePSADMINCommand-LineInterface-c07e70.html
    for a list of psadmin.exe commands.
.LINK
    Using the TMADMIN Command-Line Interface
    http://docs.oracle.com/cd/E26665_01/tuxedo/docs11gr1/rfcm/rfcmd.html#wp1971834
#> 
function Invoke-TMAdmin
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    Param
    (
        # The full path the the TUXDIR on the machine
        # See http://docs.oracle.com/cd/E26665_01/tuxedo/docs11gr1/install/inspin.html#wp1017599
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage ="Path to the Tuxedo application. (Like C:\Oracle\Weblogic\tuxedo11gR1_VS2010)",
                   Position=0)]
        [ValidateScript({Validate_TUXDIR $_})]
        $TUXDIR,

        # The full path to the PS_CFG_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        [String]
        $Domain,

        # The command which will be executed through PSAdmin
        # This string will be passed as an argument to PSAdmin.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=4)]
        [ValidateNotNullOrEmpty()]
        [String[]]
        $Command
    )

    Begin
    {
    }
    Process
    {
        # Set up required environment variables
        $env:TUXDIR = $TUXDIR
        $env:PS_CFG_HOME = $PS_CFG_HOME
        $env:TUXCONFIG = "$PS_CFG_HOME\appserv$(if($ServerType -eq "PrcsScheduler"){"\prcs"})\$Domain\PSTUXCFG"

        # Build the file with the list of commands
        $commandFile = "tmadmin$(Get-Date -Format "yyyyMMddhhmmss").txt"
        $commandFilePath = "$env:TEMP\$commandFile"

        # Describes which commands will be run
        Write-Verbose "Running: $TUXDIR\bin\tmadmin.exe 2>&1 < $commandFilePath"
        Write-Verbose "Command: $Command"

        # Write the file
        Add-Content -Value "$Command" -Path $commandFilePath
        
        # Execute tmadmin with the commands
        # Commands to tmadmin must be piped into the executable
        $result = Get-Content $commandFilePath | cmd /c "$TUXDIR\bin\tmadmin.exe 2>&1"

        Write-Verbose $result

        # Clean up the temporary command file
        Remove-Item $commandFilePath

        return $result
    }
    End
    {
    }
}